package com.jdy.haoduoaiteacher.adapter;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jdy.haoduoaiteacher.activity.BaseActivity;
import com.jdy.haoduoaiteacher.model.BabyHabit;
import com.jdy.haoduoaiteacher.util.Tools;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

//import static com.jdy.haoduoaiteacher.R.id.tv_title;

@SuppressLint("InflateParams")
public class BabyHabitAdapter extends BaseAdapter {

  private BaseActivity mContext;
  private ArrayList<BabyHabit> mList;
  private LayoutInflater mInflter;
  DisplayImageOptions options;

  private SparseArray<View> cvArray = new SparseArray<>();

  public BabyHabitAdapter(BaseActivity context,
                                 ArrayList<BabyHabit> list) {
    this.mContext = context;
    this.mList = list;
    mInflter = LayoutInflater.from(mContext);
    // this.mType = type;

    options = new DisplayImageOptions.Builder()
        .showImageOnFail(R.drawable.default_img_listitem) // 下载 失败 显示的 图片
        .cacheInMemory(true) // 缓存
        .bitmapConfig(Bitmap.Config.RGB_565) // 图片格式
        .build();

    ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
        mContext).writeDebugLogs().build();
    ImageLoader.getInstance().init(configuration);
  }

  @Override
  public int getCount() {
    return mList.size();
  }

  @Override
  public Object getItem(int position) {
    return mList.get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }



  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    BabyHabit itemVo = mList.get(position);
    ViewHolder holder;

    convertView = cvArray.get(position);

    if (convertView == null) {
      holder = new ViewHolder();
      convertView = mInflter.inflate(R.layout.babyhabit_item_teacher, null);
//      holder.iv_channelicon = (ImageView) convertView
//          .findViewById(R.id.iv_channelicon);
      holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
      holder.tv_time = (ImageView) convertView.findViewById(R.id.tv_time);
      holder.text1 = (TextView) convertView.findViewById(R.id.text1);
    //  holder.rel_root22 = convertView.findViewById(R.id.rel_root22);
      convertView.setTag(holder);
      cvArray.put(position, convertView);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    if (Tools.isNotEmpty(itemVo)) {
      holder.tv_title.setText(itemVo.name);
   //   holder.tv_time.setText(itemVo.time.substring(0,5));
    }


    String label = mList.get(position).date;
    if (position == 0//检查列表项起始字母是否发生了改变，如果发生改变，该列表项就是分段中的第一项，修改分段标头的内容并显示该分段标头,否则隐藏
            || !mList.get(position - 1).date.equalsIgnoreCase(label)) {
      holder.text1.setVisibility(View.VISIBLE);

      holder.text1.setText(Tools.getWeek(label) + " " + label.substring(0,4)
              + "年" + label.substring(5,7) + "月" + label.substring(8,label.length()) + "日");
    } else {
      holder.text1.setVisibility(View.GONE);//隐藏分段标头
    }

    if (itemVo.checked == 1) {
                  Drawable db2 = mContext.getResources().getDrawable(
                R.drawable.green_circle_icon);
      holder.tv_title.setCompoundDrawablesWithIntrinsicBounds(db2,
                null, null, null);
      holder.tv_time.setImageResource(R.drawable.red_flower);
    } else {
      Drawable db2 = mContext.getResources().getDrawable(
              R.drawable.blue_circle_icon);
      holder.tv_title.setCompoundDrawablesWithIntrinsicBounds(db2,
              null, null, null);
      holder.tv_time.setImageResource(R.drawable.gray_fllower);
    }


//    holder.rel_root22.setOnLongClickListener(new View.OnLongClickListener() {
//      @Override
//      public boolean onLongClick(View v) {
//        showPopMenu(v);
//        return true;
//      }
//    });

//    if (Tools.isNotEmpty(itemVo)) {
//      if (Tools.isNotEmpty(itemVo.catalog)) {
//        if (itemVo.catalog.contains("cartoon")
//            || itemVo.catalog.contains("radio")) {
//          holder.tv_title.setText(itemVo.album_name);
//        } else {
//          holder.tv_title.setText(itemVo.name);
//        }
//      } else {
//        holder.tv_title.setText(itemVo.album_name);
//      }
//
//      ImageLoader.getInstance().displayImage(itemVo.cover,
//          holder.iv_channelicon, options);
  //  }

    return convertView;
  }

  private static class ViewHolder {
    private TextView tv_title, text1;
    private ImageView tv_time;
 //   private View rel_root22;
  //  private ImageView iv_channelicon;

  }

}
